using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Portal.Models;
using ST.Application.DepartmentServices;
using ST.Application.DepartmentServices.Dtos;

namespace Portal.Controllers
{
    public class DepartmentController : ControllerBase
    {
        private readonly IDepartmentService _departmentService;

        public DepartmentController(IDepartmentService departmentService)
        {
            _departmentService = departmentService;
        }
        // GET
        public IActionResult Index()
        {
            return View();
        }
        
        [HttpGet]
        public async Task<IActionResult> GetTreeData()
        {
            var dtls = await _departmentService.GetAllListAsync();
            var treeModels = dtls.Select(dto => new TreeModel() {Id = dto.Id.ToString(), Text = dto.Name, Parent = dto.ParentId == Guid.Empty ? "#" : dto.ParentId.ToString()}).ToList();
            return Json(treeModels);
        }
        public async Task<IActionResult> GetChildrenByParent(Guid parentId, int startPage, int pageSize)
        {
            var result = await _departmentService.GetChildrenByParentAsync(parentId, startPage, pageSize);
            return Json(result);
        }
        
        public async Task<IActionResult> Edit(DepartmentDto dto)
        {
            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = GetModelStateError()
                });
            }

            var result = false;
            if (await _departmentService.GetAsync(dto.Id) == null)
            {
                result = await _departmentService.InsertAsync(dto);
            }
            else
            {
                result = await _departmentService.UpdateAsync(dto);
            }

            return Json(result ? new { Result = "Success" } : new { Result = "Faild" });
        }

        public  async Task<IActionResult> DeleteMuti(string ids)
        {
            string[] idArray = ids.Split(',');
            try
            {
                var delIds = idArray.Select(Guid.Parse).ToList();
                await _departmentService.DeleteBatchAsync(delIds);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public  async Task<IActionResult> Delete(Guid id)
        {
            try
            {
                await _departmentService.DeleteAsync(id);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public  async Task<IActionResult> Get(Guid id)
        {
            var dto = await _departmentService.GetAsync(id);
            return Json(dto);
        }
    }
}